﻿using System.Linq.Expressions;
using AutoMapper;
using CozeDomain.Entities;
using CozeDomain.IRepositories;
using CozeDomain.IServices;
using CozeDto.ConversationDtos;
using CozeDto.MyCollectionDtos;
using D.Util.UtilCache;
using D.Util.UtilNLog;
using D.UtilCore.Dtos;
using D.UtilCore.JwtUtil;
using D.UtilFreesqlOrm;
using Microsoft.AspNetCore.Http;


namespace CozeDomain.Services
{
    public class ConversationService : BaseService<ConversationEntity>, IConversationService
    {
        private readonly IMapper _mapper;
        private readonly ICacheService _cache;
        private readonly IJwtService _tokenService;
        private readonly INLogService _nlogService;
        private readonly IConversationEntityRepository _repository;
        private readonly IHttpContextAccessor _httpContextAccessor;

        /// <summary>
        /// 构造注入
        /// </summary>
        public ConversationService(IMapper mapper, ICacheService cache, IJwtService tokenService, INLogService nlogService, IHttpContextAccessor httpContextAccessor,
            IConversationEntityRepository repository)
        {
            // 用于base方法
            base._baseDal = repository;
            this._repository = repository;
            _cache = cache;
            _mapper = mapper;
            _nlogService = nlogService;
            _tokenService = tokenService;
            _httpContextAccessor = httpContextAccessor;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="reqModel"></param>
        /// <returns></returns>
        public async Task<ApiResult<int>> AddConversationAsync(ReqAddConversation reqModel)
        {
            var insertEntity = _mapper.Map<ReqAddConversation, ConversationEntity>(reqModel);
            insertEntity.DeleteFlag = false;
            insertEntity.CreateTime = DateTime.Now;
            insertEntity.Id = D.UtilFreesqlOrm.FreesqlOrm.NewId;

            var dbres = await _repository.AddAsync(insertEntity);

            return dbres.NormalResponse();
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns> 
        public async Task<ApiResult<int>> DeleteConversationAsync(List<string> ids)
        {
            var dbres = await _repository.DeleteAsync(m => ids.Contains(m.Id));
            return dbres.NormalResponse();
        }
  
        /// <summary>
        /// 查询列表
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="domain"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>

        public async Task<ApiResult<List<RepConversationInfo>>> QueryConversationListAsync(string userId)
        {
            var res = await _repository.QueryConversationListAsync(userId);
            return res.GetResponse();
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="reqQueryConversationPage"></param>
        /// <returns></returns>
        public async Task<ApiResult<PageModel<RepConversationInfo>>> QueryConversationPagesAsync(ReqQueryConversationPage reqQueryConversationPage)
        {
            Expression<Func<ConversationEntity, bool>> where = null;

            if (!string.IsNullOrEmpty(reqQueryConversationPage.UserId))
            {
                where = m => m.UserId == reqQueryConversationPage.UserId;
            }

            if (!string.IsNullOrEmpty(reqQueryConversationPage.ProjectId))
            {
                where = where.And(m => m.ProjectId == reqQueryConversationPage.ProjectId);
            }

            var dbres = await _repository.GetPagesAsync(reqQueryConversationPage.PageSize, reqQueryConversationPage.PageNo, where, m => m.CreateTime);
            var resData = _mapper.Map<List<ConversationEntity>, List<RepConversationInfo>>(dbres.Data);
            var res = new PageModel<RepConversationInfo>()
            {
                PageNo = dbres.PageNo,
                PageSize = dbres.PageSize,
                TotalCount = dbres.TotalCount,
                TotalPage = dbres.TotalPage,
                Data = resData
            };

            return res.GetResponse();
        }
    }
}
